import pygame, time
from pygame.locals import *
Window_width=326#屏幕宽
Window_height=562#屏幕高
Bird_height=30#鸟高度差不多
Bird_width=40#鸟宽度差不多
Tree_width=20#树宽度
Tree_height=248#数高度
pygame.init()
def game_over(screen, text):
    font_size = 20
    font = pygame.font.SysFont('arial', font_size)
    screen.blit(font.render(text, True, (255, 255, 255), (0, 0, 0)),
                (60, 30 * 4))
class Xn(object):

    def __init__(self,screen,direction):
        self.x=10
        self.y=320

        self.screen=screen
        self.direction=direction
        self.image_name='./xntupian/小鸟01.png'
        self.image=pygame.image.load(self.image_name).convert()
        self.bullet_list=[]
    def drop(self):
        self.y+=0.4
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
    def move_down(self):
             self.y+=20
    def move_up(self):
        self.y -= 20

class Tree(object):
    is_treedie=True
    def __init__(self,screen):
        self.xx=300
        self.yy=335
        self.screen=screen
        self.image_name='./xntupian/tree.png'
    def display(self):
        # for i in self.list:
               self.image = pygame.image.load(self.image_name).convert()
               self.screen.blit(self.image, (self.xx, self.yy))
    def move_tree(self):
        self.xx-=2
        if self.xx<=-20:
            self.xx=500
            return self.xx
class Tree1(Tree):
    def __init__(self,screen):
        super(Tree1,self).__init__(screen)
        self.xx= 500
        self.yy = 2
        self.image_name = './xntupian/tree01.png'
        self.image=pygame.image.load(self.image_name).convert()
def main():
    screen=pygame.display.set_mode((Window_width,Window_height))
    image_name='./xntupian/背景图片03.jpg'
    background=pygame.image.load(image_name).convert()
    xn=Xn(screen,direction='down')
    tree=Tree(screen)
    tree1=Tree1(screen)
    pygame.display.update()
    while True:
            screen.blit(background, (0, 0))
            xn.display()
            xn.move_down()
            xn.drop()
            xn.move_up()
            # 父类
            tree.display()
            tree.move_tree()
            # 子类
            tree1.move_tree()
            tree1.display()
            bird_y = xn.y
            t1_x = tree.xx
            t2_x = tree1.xx
            asd=''
            if t1_x>=(Tree_width-10) and t1_x<=(10+Bird_width):
                if bird_y>=Tree_height+50:
                    game_over(screen, 'Press Enter To Start!')
                    pygame.display.update()
                    return asd==QUIT
            elif t2_x>=(Tree_width-10)and t2_x<=(10+Bird_width):
                if bird_y<=110:
                    game_over(screen, 'Press Enter To Start!')
                    pygame.display.update()
                    return asd==QUIT
            def key_control(player_tmp):
                for event in pygame.event.get():
                    # 判断是否是点击了退出按钮
                    if asd == QUIT:
                        print("exit")
                        exit()
                    elif event.type == KEYDOWN:
                        if event.key == K_w or event.key == K_UP:
                            print('up')
                            player_tmp.move_up()
                        elif event.key == K_s or event.key == K_DOWN:
                            print('down')
                            player_tmp.move_down()
            pygame.display.update()
            key_control(xn)
            time.sleep(0.01)
if __name__=="__main__":
          main()
